package com.adl.controller;

import com.adl.MVCLocaleResolver;
import com.adl.dao.PersonDAO;
import com.adl.entity.Person;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import java.util.Date;
import java.util.Locale;

@Controller
@RequestMapping("/")
public class TestController implements ServletContextAware {
    @Resource
    private PersonDAO personDAO;

    private ServletContext servletContext;

    @RequestMapping
    @ResponseBody
    public Object index() {
        System.out.println(personDAO);
        Person person = new Person();
        person.setName("---" + System.currentTimeMillis());
        personDAO.save(person);
        Person byId = personDAO.findById(1);
        System.out.println(byId.getName());
        return "welcome " + new Date();
    }

    @RequestMapping("/add")
    @ResponseBody
    public Object add() {
        System.out.println(personDAO);
        Person person = new Person();
        person.setName("add-" + System.currentTimeMillis());
        personDAO.save(person);
        return person;
    }
    @Autowired
    ResourceBundleMessageSource messageSource;
    @Autowired
    SessionLocaleResolver       localeResolver;

    @RequestMapping("/test")
    public Object test() {
        System.out.println("访问：/test");
        ServletRequestAttributes requestAttributes  = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        WebApplicationContext    applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        String[]                 beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        System.out.println(StringUtils.join(beanDefinitionNames,"\n"));

//        MVCLocaleResolver localeResolver =(MVCLocaleResolver) applicationContext.getBean( LocaleResolver.class);
//        Locale            resolveLocale         = localeResolver.resolveLocale(requestAttributes.getRequest());

        String            message        = messageSource.getMessage("user.name", null, localeResolver.resolveLocale(requestAttributes.getRequest()));
        System.out.println(message);
        return "/test";
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
}
